﻿using BaseData.ApplicationLib.Commands;
using BaseData.ApplicationLib.Queries;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.WebApi.Controllers;

[ApiController]
public class WorkstationController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly IWorkstationQueries _customerQueries;

    public WorkstationController(IMediator mediator,
        IWorkstationQueries unitQueries)
    {
        _mediator = mediator;
        _customerQueries = unitQueries;
    }

    [HttpGet(ApiRoutes.Workstation.QueryWorkstations)]
    public async Task<IActionResult> Get([FromQuery] WorkstationSearchRequestDto model)
    {
        if (model.Page > 0 || model.Limit > 0)
        {
            var result = await _customerQueries.QueryWorkstationsByPage(model);
            return Ok(result);
        }
        else
        {
            var result = await _customerQueries.QueryWorkstations(model);
            return Ok(result);
        }
    }

    [HttpGet(ApiRoutes.Workstation.GetWorkstation)]
    public async Task<IActionResult> GetWorkstation(int id)
    {
        var result = await _customerQueries.GetWorkstation(id);
        return Ok(result);
    }

    [HttpPost(ApiRoutes.Workstation.CreateWorkstation)]
    public async Task<IActionResult> Post(CreateWorkstationCommand command)
    {
        var result = await _mediator.Send(command);
        return Ok(result);
    }

    [HttpPut(ApiRoutes.Workstation.UpdateWorkstation)]
    public async Task<IActionResult> Put(int id, [FromBody] UpdateWorkstationCommand command)
    {
        command.Id = id;
        var result = await _mediator.Send(command);
        return Ok(result);
    }
}